/* SQUIB - Scala's Quirky User Interface Builder 
    Copyright (C) 2008 Tim Dalton

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
*/

import scala.collection.mutable.{HashMap}
    
package tfd.scala.squib {

import java.awt.Component
import java.awt.event.{WindowEvent, WindowListener}
import java.util.EventObject

    trait FiresWindowEvents[T <: {  def addWindowListener(ml:WindowListener) }] { self:FiresEventObjects with IdableTrait =>
        self.eventHandlers += (windowEvents _).asInstanceOf[(Object, HashMap[String, EventObject => Unit]) => Unit]

        def windowEvents(comp: T, events:HashMap[String, EventObject => Unit]):T = {
            val handler = new DelegateWindowEventHandler(events);
            if (handler.hasEvents) {
                comp.addWindowListener(handler)
            }
            comp
        }

        class DelegateWindowEventHandler(events: HashMap[String, EventObject => Unit]) extends WindowListener {
            private val windowActivatedDelegate:Option[WindowEvent => Unit] = events.get("windowactivated");
            private val windowClosedDelegate:Option[WindowEvent => Unit] = events.get("windowclosed");
            private val windowClosingDelegate:Option[WindowEvent => Unit] = events.get("windowclosing");
            private val windowDeactivatedDelegate:Option[WindowEvent => Unit] = events.get("windowdeactivated");
            private val windowDeiconifiedDelegate:Option[WindowEvent => Unit] = events.get("windowdeiconified");
            private val windowIconifiedDelegate:Option[WindowEvent => Unit] = events.get("windowiconified");
            private val windowOpenedDelegate:Option[WindowEvent => Unit] = events.get("windowopened");

            lazy val hasEvents = !(windowActivatedDelegate.isEmpty
                                   && windowClosedDelegate.isEmpty
                                   && windowClosingDelegate.isEmpty
                                   && windowDeactivatedDelegate.isEmpty
                                   && windowDeiconifiedDelegate.isEmpty
                                   && windowIconifiedDelegate.isEmpty
                                   && windowOpenedDelegate.isEmpty)
            
            override def windowActivated(we: WindowEvent) {
                if (!windowActivatedDelegate.isEmpty) windowActivatedDelegate.get.apply(we)
            }

            override def windowClosed(we: WindowEvent) {
                if (!windowClosedDelegate.isEmpty) windowClosedDelegate.get.apply(we)
            }

            override def windowClosing(we: WindowEvent) {
                if (!windowClosingDelegate.isEmpty) windowClosingDelegate.get.apply(we)
            }

            override def windowDeactivated(we: WindowEvent) {
                if (!windowDeactivatedDelegate.isEmpty) windowDeactivatedDelegate.get.apply(we)
            }

            override def windowDeiconified(we: WindowEvent) {
                if (!windowDeiconifiedDelegate.isEmpty) windowDeiconifiedDelegate.get.apply(we)
            }

            override def windowIconified(we: WindowEvent) {
                if (!windowIconifiedDelegate.isEmpty) windowIconifiedDelegate.get.apply(we)
            }

            override def windowOpened(we: WindowEvent) {
                if (!windowOpenedDelegate.isEmpty) windowOpenedDelegate.get.apply(we)
            }
        }
    }
}
